package com.example.healkep.fragment.bg;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.example.healkep.R;

import com.example.healkep.echarts.EchartOptionUtil;
import com.example.healkep.echarts.EchartView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FragmentDay extends Fragment {

    private double[] bg = new double[10];
    private EchartView lineChart,pieChart;
    private TextView textView1, textView2, textView3;
    private double bg_max = 0, bg_min = 0;
    private int sum;  //记录七日测量总数
    private double bg_sum, bg_ave = 0; // 配合sum计算总平均值
    private int count_high,count_low,count_ave;

    @Override
    public View onCreateView(final LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        final Intent intent = getActivity().getIntent();
        bg = intent.getDoubleArrayExtra("bg");
        System.out.println("bg: ");
        System.out.println(bg[0]);

        bg_max = bg[0];
        for(int i=0;i<10;i++){
            if(bg[i]!=0) {
                bg_min = bg[i];
                break;
            }
        }
        for(int i=0; i<10; i++){
            if(bg[i]!= 0){
                bg_sum += bg[i];
                sum++;
            }
            if(bg[i] > bg_max) bg_max = bg[i];
            if(bg[i] < bg_min && bg[i]!=0) bg_min = bg[i];
        }
        //空腹：4.4-6.1   餐后1h：6.7-8.3    餐后2h：5.0-7.2   餐后3h：4.4-6.7
        if(bg[0] < 4.4 && bg[0]!=0) count_low++;
        else if(bg[0] > 6.1) count_high++;
        else count_ave++;
        if(bg[1] < 6.7 && bg[1]!=0) count_low++;
        else if(bg[1] > 8.3) count_high++;
        else count_ave++;
        if(bg[2] < 5.0 && bg[2]!=0) count_low++;
        else if(bg[2] > 7.2) count_high++;
        else count_ave++;
        if(bg[3] < 4.4 && bg[3]!=0) count_low++;
        else if(bg[3] > 6.1) count_high++;
        else count_ave++;
        if(bg[4] < 6.7 && bg[4]!=0) count_low++;
        else if(bg[4] > 8.3) count_high++;
        else count_ave++;
        if(bg[5] < 5.0 && bg[5]!=0) count_low++;
        else if(bg[5] > 7.2) count_high++;
        else count_ave++;
        if(bg[6] < 4.4 && bg[6]!=0) count_low++;
        else if(bg[6] > 6.1) count_high++;
        else count_ave++;
        if(bg[7] < 6.7 && bg[7]!=0) count_low++;
        else if(bg[7] > 8.3) count_high++;
        else count_ave++;
        if(bg[8] < 5.0 && bg[8]!=0) count_low++;
        else if(bg[8] > 7.2) count_high++;
        else count_ave++;
        if(bg[9] < 4.4 && bg[9]!=0) count_low++;
        else if(bg[9] > 6.7) count_high++;
        else count_ave++;

        if(sum != 0) bg_ave = bg_sum/sum;
        String ave = String.format("%.1f",bg_ave);
        String max = Double.toString(bg_max);
        String min = Double.toString(bg_min);

        final View view = inflater.inflate(R.layout.bg_fragment_day,
                container, false);

        textView1 = view.findViewById(R.id.bg_ave);
        textView2 = view.findViewById(R.id.bg_high);
        textView3 = view.findViewById(R.id.bg_low);

        textView1.setText(ave);
        textView2.setText(max);
        textView3.setText(min);

        initView(view);
        return view;
    }

    private void initView(View view){
        lineChart = view.findViewById(R.id.lineChart);
        lineChart.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //最好在h5页面加载完毕后再加载数据，防止html的标签还未加载完成，不能正常显示
                refreshLineChart();
            }
        });
        pieChart = view.findViewById(R.id.PieChart);
        pieChart.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //最好在h5页面加载完毕后再加载数据，防止html的标签还未加载完成，不能正常显示
                refreshPieChart();
            }
        });
    }

    private void refreshLineChart(){
        Object[] x = new Object[]{
                "空腹", "早餐后1h", "早餐前2h", "午餐前", "午餐后1h", "午餐后2h", "晚餐前", "晚餐后1h", "晚餐后2h", "睡前"
        };
        Object[] y = new Object[]{
                bg[0], bg[1], bg[2], bg[3], bg[4], bg[5], bg[6], bg[7], bg[8], bg[9]
        };
        lineChart.refreshEchartsWithOption(EchartOptionUtil.getLineChartOptions(x, y));
    }

    private void refreshPieChart(){

        String high = "偏高: " + Integer.toString(count_high)+ " 次";
        String low = "偏低: " + Integer.toString(count_low)+ " 次";
        String ave = "正常: " + Integer.toString(count_ave)+ " 次";
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        HashMap hashMap1 = new HashMap();
        hashMap1.put("value", count_high);
        hashMap1.put("name", high);
        HashMap hashMap2 = new HashMap();
        hashMap2.put("value", count_low);
        hashMap2.put("name", low);
        HashMap hashMap3 = new HashMap();
        hashMap3.put("value", count_ave);
        hashMap3.put("name", ave);
        //pie.data(hashMap,hashMap1,hashMap2);

        data.add(hashMap1);
        data.add(hashMap2);
        data.add(hashMap3);

        pieChart.refreshEchartsWithOption(EchartOptionUtil.getPieChartOptions(data));
    }

}


